package com.jiamao.goplay.ui.settings.vm;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.jiamao.goplay.data.DataRepository;
import com.kongzue.dialog.util.DialogSettings;
import com.tamsiree.rxkit.view.RxToast;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.ToastUtils;
import skin.support.SkinCompatManager;

public class ColorVM extends BaseViewModel<DataRepository> {
    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt();

    public ObservableBoolean btnClickEndbel = new ObservableBoolean();

    public ObservableField<String> smscode = new ObservableField<>("");

    //封装一个界面发生改变的观察者
    public ColorVM.UIChangeObservable uc = new ColorVM.UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
    }

    public ColorVM(@NonNull Application application, DataRepository repository) {
        super(application, repository);
        //从本地取得数据绑定到View层
        userName.set(model.getUserName());
        password.set(model.getPassword());


    }
    //登录按钮的点击事件
    public BindingCommand backOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });


    //登录按钮的点击事件
    public BindingCommand redCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            SkinCompatManager.getInstance().loadSkin("red", new SkinCompatManager.SkinLoaderListener() {
                @Override
                public void onStart() {

                }

                @Override
                public void onSuccess() {
                    Log.d("HTML","onSuccess");
                }

                @Override
                public void onFailed(String errMsg) {
                Log.d("HTML","onFailed");
                }
            },SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
        }
    });

    //登录按钮的点击事件
    public BindingCommand blackCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            SkinCompatManager.getInstance().loadSkin("night", new SkinCompatManager.SkinLoaderListener() {
                @Override
                public void onStart() {

                }

                @Override
                public void onSuccess() {
                    Log.d("HTML","onSuccess");
                    DialogSettings.theme = DialogSettings.THEME.DARK;
                }

                @Override
                public void onFailed(String errMsg) {
                    Log.d("HTML","onFailed");
                }
            }, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
            //   startContainerActivity(SettingsFragment.class.getCanonicalName());
        }
    });

    //登录按钮的点击事件
    public BindingCommand defaultCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            DialogSettings.theme = DialogSettings.THEME.LIGHT;
            SkinCompatManager.getInstance().restoreDefaultTheme();


        }
    });



    //登录按钮的点击事件
    public BindingCommand aboutCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

              // startContainerActivity(AboutFragment.class.getCanonicalName());
        }
    });

    //登录按钮的点击事件
    public BindingCommand aaCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            SkinCompatManager.getInstance().loadSkin("aa.skin", SkinCompatManager.SKIN_LOADER_STRATEGY_ASSETS);

            // startContainerActivity(AboutFragment.class.getCanonicalName());
        }
    });
    //登录按钮的点击事件
    public BindingCommand logoutCommd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            RxToast.showToastShort("退出登录");
            finish();
           // startContainerActivity(AboutFragment.class.getCanonicalName());
        }
    });
    public void addTextEditFoush(){

        if (userName.get().length()== 11 && password.get().length()>=6){
            btnClickEndbel.set(true);
        }else {
            btnClickEndbel.set(false);
        }
    }

    /**
     * 网络模拟一个登陆操作
     **/
    private void login() {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }
//        RxPermissions rxPermissions = new RxPermissions((Activity) context);
//        RetrofitClient.getInstance().create(ApiService.class)
//                .demoGet()
//                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
//                .compose(RxUtils.schedulersTransformer())  // 线程调度
//                .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
//                .subscribe(new Consumer<BaseResponse<DemoEntity>>() {
//                    @Override
//                    public void accept(BaseResponse<DemoEntity> response) throws Exception {
//
//                    }
//                }, new Consumer<ResponseThrowable>() {
//                    @Override
//                    public void accept(ResponseThrowable throwable) throws Exception {
//
//                    }
//                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
